package net.mikoo.seals.boss.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.boss.biz.services.DevicesModelServices;
import net.mikoo.seals.boss.common.BaseController;
import net.mikoo.seals.boss.common.exception.ParametersException;
import net.mikoo.seals.boss.model.DevicesModel;



@Controller
@RequestMapping("/admin/model")
public class DevicesModelController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(DevicesModelController.class);
	
	@Autowired
	private DevicesModelServices devicesModelServices;
	
	@RequestMapping("/list")
	public String list(HttpServletRequest request, Model model) {
		List<DevicesModel> models = devicesModelServices.getList();
		model.addAttribute("models", models);
		return resultView(ADMIN_VIEW, "model/list");
	}

	@RequestMapping("/form")
	public String form() {
		
		return resultView(ADMIN_VIEW, "model/form");
	}
	
	@RequestMapping("/create")
	@ResponseBody
	public Map<String,Object> create(DevicesModel devicesModel,HttpServletRequest request, Model model) {
		try {
			devicesModelServices.save(devicesModel);
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
	}
	
	
	/**
	 * 设备升级
	 * @param model
	 * @return
	 */
	@RequestMapping("/upgrade")
	public String upgrade(String modelId, Model model) {
		
		DevicesModel devicesModel = devicesModelServices.getInfoById(modelId);
		model.addAttribute("devicesModel", devicesModel);
		return resultView(ADMIN_VIEW, "model/upgrade");
	}
	
	/**
	 * 升级发布
	 * @param modelId
	 * @param model
	 * @return
	 */
	@RequestMapping("/publish")
	@ResponseBody
	public Map<String,Object> publish(DevicesModel devicesModel, Model model) {
		
		try {
			devicesModelServices.publish(devicesModel);
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
		
		
		
	}
	
	

	@RequestMapping("/edit")
	public String edit(String devicesModelId, Model model) {
		DevicesModel devicesModel = devicesModelServices.getInfoById(devicesModelId);
		model.addAttribute("devicesModel", devicesModel);
		return resultView(ADMIN_VIEW, "devicesModel/edit");
	}
	
	@RequestMapping("/modify")
	public Map<String,Object> modify(DevicesModel devicesModel, Model model) {
		
		try {
			devicesModelServices.modify(devicesModel);
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
	}

}
